package com.syn.michat.controller;

import com.alibaba.fastjson.JSON;
import com.syn.michat.constant.FriendContants;
import com.syn.michat.constant.MessageContants;
import com.syn.michat.im.service.MessageService;
import com.syn.michat.model.ResponseEntity;
import com.syn.michat.pojo.Friend;
import com.syn.michat.pojo.User;
import com.syn.michat.service.FriendsService;
import com.syn.michat.util.ResponseErrorUtil;
import com.syn.michat.util.exception.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/friends",produces = "application/json;charset=UTF-8")
public class FriendsController {

    @Autowired
    private FriendsService friendsService;
    @Autowired
    private MessageService messageService;

    /**
    * @Description: 好友申请
    * @Params: [request, userId, remark]
    * @return: java.lang.String
    * @Author: 申铭
    * @Date: 2021/12/17
    */
    @PostMapping("/request")
    public String request(HttpServletRequest request, @RequestParam String userId, String remark) {
        User user = (User) request.getAttribute("user");

        Friend friend = new Friend();
        friend.setUserId(userId);
        friend.setFriendId(user.getUserId());
        friend.setStatus(FriendContants.REQUEST);
        if (remark == null) {
            friend.setRemark("此人很懒，没有留下任何信息。");
        } else {
            friend.setRemark(remark);
        }

        try {
            HashMap<String, Object> map = new HashMap();

            int result = friendsService.request(friend);
            if (result == FriendContants.ADDED_SUCCESSFULLY) {
                map.put("result", "2");
            } else {
                // 给被申请好友者发送socket消息
                messageService.sendMsg(user.getUserId(), userId, MessageContants.REQUEST_FRIEND);
                map.put("result", "1");
            }

            ResponseEntity res = new ResponseEntity(map);
            return JSON.toJSONString(res);
        } catch (Exception e) {
            return JSON.toJSONString(ResponseErrorUtil.convertException(e));
        }
    }

    /**
    * @Description: 好友申请列表
    * @Params: [request]
    * @return: java.lang.String
    * @Author: 申铭
    * @Date: 2021/12/17
    */
    @GetMapping("/request")
    public String requestList(HttpServletRequest request) {
        User user = (User) request.getAttribute("user");

        try {
            List<Friend> list = friendsService.queryAllApplyRecords(user.getUserId());

            HashMap<String, Object> map = new HashMap();
            map.put("requests", list);
            ResponseEntity res = new ResponseEntity(map);
            return JSON.toJSONString(res);
        } catch (ClientException e) {
            return JSON.toJSONString(ResponseErrorUtil.convertException(e));
        }
    }

    @PostMapping("/approval")
    public String approvalFriend(HttpServletRequest request, @RequestParam String targetId, @RequestParam int isAgree) {
        User user = (User) request.getAttribute("user");

        try {
            friendsService.approvalFriend(user.getUserId(), targetId, isAgree);
            // 同意后，给对方发送成为好友的socket消息
            if (isAgree == 1) {
                messageService.sendMsg(user.getUserId(), targetId, MessageContants.BECOME_FRIEND);
            }
        }  catch (Exception e) {
            return JSON.toJSONString(ResponseErrorUtil.convertException(e));
        }

        return JSON.toJSONString(new ResponseEntity());
    }

    /**
    * @Description: 好友列表
    * @Params: [request]
    * @return: java.lang.String
    * @Author: 申铭
    * @Date: 2021/12/17
    */
    @GetMapping("/list")
    public String friendsList(HttpServletRequest request) {
        User user = (User) request.getAttribute("user");

        try {
            List<Friend> list = friendsService.queryAllFriends(user.getUserId());

            HashMap<String, Object> map = new HashMap();
            map.put("friends", list);
            ResponseEntity res = new ResponseEntity(map);
            return JSON.toJSONString(res);
        } catch (ClientException e) {
            return JSON.toJSONString(ResponseErrorUtil.convertException(e));
        }
    }

    @PostMapping("/delete")
    public String deleteFriend(HttpServletRequest request, @RequestParam String userId) {
        User user = (User) request.getAttribute("user");

        try {
            friendsService.deleteFriend(user.getUserId(), userId);
        }  catch (ClientException e) {
            return JSON.toJSONString(ResponseErrorUtil.convertException(e));
        }

        return JSON.toJSONString(new ResponseEntity());
    }

}
